import { AppRouteRecord } from '@/types/router'
import request from '@/utils/http'

// 获取用户列表
export function fetchGetUserList(params: Api.SystemManage.UserSearchParams) {
  return request.get<Api.SystemManage.UserList>({
    url: '/api/user/list',
    params
  })
}

// 新增用户
export function createUser(data: any) {
  return request.post<Api.SystemManage.UserListItem>({
    url: '/api/user/add',
    data
  })
}

// 更新用户
export function updateUser(data: any) {
  return request.post<Api.SystemManage.UserListItem>({
    url: '/api/user/update',
    data
  })
}

// 删除用户
export function deleteUser(id: number) {
  return request.post<boolean>({
    url: `/api/user/delete/${id}`
  })
}

// 批量删除用户
export function batchDeleteUsers(ids: number[]) {
  return request.post<boolean>({
    url: '/api/user/delete/batch',
    data: ids
  })
}

// 获取角色列表
export function fetchGetRoleList(params: Api.SystemManage.RoleSearchParams) {
  return request.get<Api.SystemManage.RoleList>({
    url: '/api/role/list',
    params
  })
}

// 创建角色
export function createRole(data: Api.SystemManage.RoleListItem) {
  return request.post<Api.SystemManage.RoleListItem>({
    url: '/api/role/add',
    data
  })
}

// 更新角色
export function updateRole(id: string, data: Api.SystemManage.RoleListItem) {
  return request.post<Api.SystemManage.RoleListItem>({
    url: `/api/role/update`,
    data
  })
}

// 删除角色
export function deleteRole(id: string) {
  return request.post<boolean>({
    url: `/api/role/delete/${id}`
  })
}

// 批量删除角色
export function batchDeleteRoles(ids: string[]) {
  return request.post<boolean>({
    url: '/api/role/delete/batch',
    data: ids
  })
}

// 获取角色权限ID集合
export function getRolePermissions(roleId: string) {
  return request.get<string[]>({
    url: `/api/role/${roleId}/permissions`
  })
}

// 分配角色权限
export function assignRolePermissions(roleId: string, permissionIds: string[]) {
  return request.post<void>({
    url: `/api/role/${roleId}/permissions`,
    data: permissionIds
  })
}

// 获取菜单列表
export function fetchGetMenuList() {
  return request.get<AppRouteRecord[]>({
    url: '/api/system/menus/simple'
  })
}

// 新增菜单
export function createMenu(data: any) {
  return request.post<any>({
    url: '/api/permission/add',
    data
  })
}

// 更新菜单
export function updateMenu(id: string, data: any) {
  return request.post<any>({
    url: `/api/permission/update`,
    data
  })
}

// 删除菜单
export function deleteMenu(id: string) {
  return request.post<boolean>({
    url: `/api/permission/delete/${id}`
  })
}

// 新增权限按钮
export function createAuthButton(data: any) {
  return request.post<any>({
    url: '/api/permission/auth/add',
    data
  })
}

// 更新权限按钮
export function updateAuthButton(id: string, data: any) {
  return request.post<any>({
    url: `/api/permission/auth/update`,
    data
  })
}

// 删除权限按钮
export function deleteAuthButton(id: string) {
  return request.post<boolean>({
    url: `/api/permission/auth/delete/${id}`
  })
}

// 获取所有权限
export async function fetchGetAllPermissions() {
  try {
    // 模拟接口返回的菜单数据
    // const menuData = asyncRoutes
    // 后台接口返回数据 - 直接声明为数组类型
    const menuList = await request.get<AppRouteRecord[]>({
      url: '/api/permission/list'
    })

    return menuList
  } catch (error) {
    throw error instanceof Error ? error : new Error('获取菜单失败')
  }
}

// 机构管理相关API
// 获取机构树
export async function getDeptTree() {
  try {
    const response = await request.get<any[]>({
      url: '/api/dept/tree'
    })
    return response
  } catch (error) {
    console.warn('机构树API调用失败，使用模拟数据:', error)
    // 导入模拟数据
    const { MOCK_DEPT_TREE_DATA } = await import('@/mock/temp/deptData')
    return MOCK_DEPT_TREE_DATA
  }
}

// 获取机构列表
export function getDeptList(params?: any) {
  return request.get<any[]>({
    url: '/api/dept/list',
    params
  })
}

// 根据ID获取机构
export function getDeptById(id: string) {
  return request.get<any>({
    url: `/api/dept/${id}`
  })
}

// 新增机构
export function createDept(data: any) {
  return request.post<any>({
    url: '/api/dept/add',
    data
  })
}

// 更新机构
export function updateDept(id: string, data: any) {
  return request.post<any>({
    url: `/api/dept/update`,
    data
  })
}

// 删除机构
export function deleteDept(id: string) {
  return request.post<boolean>({
    url: `/api/dept/delete/${id}`
  })
}

// 获取机构下的子机构
export async function getDeptChildren(deptId: string, params?: any) {
  try {
    const response = await request.get<any[]>({
      url: `/api/dept/${deptId}/children`,
      params
    })
    return response
  } catch (error) {
    console.warn('获取机构子机构API调用失败，使用模拟数据:', error)
  }
}

// 批量删除机构
export function batchDeleteDepts(ids: string[]) {
  return request.post<boolean>({
    url: '/api/dept/batch/delete',
    data: ids
  })
}
